package com.tuanzi.blog.web.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tuanzi.blog.model.BlogWithBLOBs;
import com.tuanzi.blog.model.Type;
import com.tuanzi.blog.model.User;
import com.tuanzi.blog.service.BlogService;
import com.tuanzi.blog.service.TagService;
import com.tuanzi.blog.service.TypeService;
import com.tuanzi.blog.vo.BlogQuery;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 雄关漫道真如铁，而今迈过从头越
 **/

@Controller
@RequestMapping("/admin")
public class BlogController {

    @Resource
    private BlogService blogService;

    @Resource
    private TypeService typeService;

    @Resource
    private TagService tagService;

    @GetMapping("/blogs")
    public String blogPage(@PageableDefault(page = 1,size = 6,sort = {"updateTime"},direction = Sort.Direction.DESC)Pageable pageable, Model model){

        PageHelper.startPage(pageable.getPageNumber(), pageable.getPageSize());
        List<BlogWithBLOBs> list = blogService.getAllBlogs(); //得到从pageNum开始的pageSize条数据
        //如果直接返回list，得到了分页的数据，如果添加下面步骤，返回pageInfo，则能得到包括list在内的分页信息
        PageInfo<BlogWithBLOBs> pageInfo = new PageInfo<>(list);
        model.addAttribute("page",pageInfo);
        model.addAttribute("types",typeService.getAllTypes());

        return "admin/blogs";
    }

    @PostMapping("/blogs/search")
    public String search(@PageableDefault(page = 1,size = 6,sort = {"updateTime"},direction = Sort.Direction.DESC)Pageable pageable, BlogWithBLOBs blog, Model model){
        if (blog.getTitle() == "" && blog.getTypeId() == null && blog.getRecommend() ==  null){
            PageHelper.startPage(pageable.getPageNumber(), pageable.getPageSize());
            List<BlogWithBLOBs> list = blogService.getAllBlogs();
            PageInfo<BlogWithBLOBs> pageInfo = new PageInfo<>(list);
            model.addAttribute("page",pageInfo);
        }else {
            PageHelper.startPage(pageable.getPageNumber(), pageable.getPageSize());
            List<BlogWithBLOBs> list = blogService.list(blog);
            PageInfo<BlogWithBLOBs> pageInfo = new PageInfo<>(list);
            model.addAttribute("page",pageInfo);
        }

        return "admin/blogs :: blogList";
    }

    @GetMapping("/blogs/input")
    public String input(Model model){
        model.addAttribute("blog",new BlogWithBLOBs());
        model.addAttribute("types",typeService.getAllTypes());
        model.addAttribute("tags",tagService.getAllTags());

        return "/admin/blogs-input";
    }

    @GetMapping("/blogs/{id}/input")
    public String update(Model model, @PathVariable Long id){
        model.addAttribute("blog",blogService.getBlog(id));
        model.addAttribute("types",typeService.getAllTypes());
        model.addAttribute("tags",tagService.getAllTags());

        return "/admin/blogs-input";
    }

    /**
     * 提交表单
     */
    @PostMapping("/blogs")
    public String post(BlogWithBLOBs blog, String tagIds, RedirectAttributes redirectAttributes, HttpSession session){
        User user = (User) session.getAttribute("user");
        blog.setUserId(user.getId());
        blog.setCreateTime(new Date());
        blog.setUpdateTime(new Date());
        blog.setTags(tagIds);
        int b = blogService.saveBlog(blog);
        if (b == 3){
            redirectAttributes.addFlashAttribute("message","添加文章 <"+blog.getTitle()+"> 失败！！！");
        }else if (b == 1){
            redirectAttributes.addFlashAttribute("message","增加文章 <"+blog.getTitle()+"> 成功！！");
        }
        else if (b == 2){
            redirectAttributes.addFlashAttribute("message","更改文章 <"+blog.getTitle()+"> 成功！！");
        }
        else if (b == 4){
            redirectAttributes.addFlashAttribute("message","更改文章 <"+blog.getTitle()+"> 失败！！");
        }

        return "redirect:/admin/blogs";
    }

    //更改博客点击跳转
    @GetMapping("/blogs/{id}/update")
    public String update(@PathVariable Long id,Model model){

        model.addAttribute("blog",blogService.getBlog(id));
        model.addAttribute("types",typeService.getAllTypes());
        model.addAttribute("tags",tagService.getAllTags());

        return "admin/blogs-input";
    }

    //删除文章
    @GetMapping("/blogs/{id}/delete")
    public String delete(@PathVariable Long id,RedirectAttributes redirectAttributes){
        BlogWithBLOBs blog = blogService.deleteBlog(id);
        if (blog != null){
            redirectAttributes.addFlashAttribute("message","这篇 <"+blog.getTitle()+"> 文章已删除!");
        }else {
            redirectAttributes.addFlashAttribute("message","这篇 <"+blog.getTitle()+"> 文章删除失败!");
        }
        return "redirect:/admin/blogs";
    }

}
